package com.itheima.controller;


import com.itheima.health.resource.upload.UploadUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("jersey")
public class JerseyController {

    @RequestMapping("upload.do")
    public String upload(@RequestParam("uploadFile")MultipartFile upload){


        try {

            //获取上传文件名称
            String filename = upload.getOriginalFilename();

            //把文件名设置成唯一的值，uuid
            String fileName = UploadUtils.generateRandonFileName(filename);

            //创建服务端的对象
            Client client = Client.create();

            //与服务器链接
            WebResource webResource = client.resource("http://192.168.138.66:9001/file-service/" + fileName);
            //上传文件
            webResource.put(upload.getBytes());

            return "redirect:/upload/success.html";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/upload/error.html";
        }
    }


}
